package com.aheizi.oa.view.action;

import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.aheizi.oa.base.BaseAction;
import com.aheizi.oa.domain.Forum;
import com.aheizi.oa.domain.PageBean;
import com.aheizi.oa.domain.Topic;
import com.opensymphony.xwork2.ActionContext;

@Controller
@Scope("prototype")
public class TopicAction extends BaseAction<Topic>{

	private Long forumId;
	
	//显示单个主题
	public String show() throws Exception {
		//准备数据:topic
		Topic topic = topicService.getById(model.getId());   // TODO 这里写forumId行不行
		ActionContext.getContext().put("topic", topic);
		
		//准备数据:replyList
//		List<Reply> replyList = replyService.findByTopic(topic);
//		ActionContext.getContext().put("replyList", replyList);
		
//		//准备分页信息 v1
//		PageBean pageBean = replyService.getPageBeanByTopic(pageNum, pageSize, topic);
//		ActionContext.getContext().getValueStack().push(pageBean);
		
		return "show";
	}
	
	//发新帖页面
	public String addUI() throws Exception {
		//准备数据
		Forum forum = forumService.getById(forumId);
		ActionContext.getContext().put("forum", forum);
		return "addUI";
	}

	/** 发表新主题 */
	public String add() throws Exception {
		// 封装
		// >> 表单参数，已经封装了title, content
		// model.setTitle(title);
		// model.setContent(content);
		model.setForum(forumService.getById(forumId));
		// >> 当前直接获取的信息
		model.setAuthor(getCurrentUser()); // 当前登录用户
		model.setIpAddr(ServletActionContext.getRequest().getRemoteAddr()); // 当前请求中的IP
		model.setPostTime(new Date()); // 当前时间

		// 保存
		topicService.save(model);

		return "toShow"; // 转到新主题的显示页面
	}
	//------------
	
	public Long getForumId() {
		return forumId;
	}

	public void setForumId(Long forumId) {
		this.forumId = forumId;
	}
	
	
}
